﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RedPanda.MusicPlayer.DataModel;
using WMPLib;

using System.ComponentModel;

using RedPanda.MusicPlayer.ViewModel;

namespace RedPanda.MusicPlayer
{
    public class MPlayer
    {
        private AudioFileViewModel currentTrack = App.mainViewModel.CurrentTrack;
        private PlaylistViewModel currentPlaylist = App.mainViewModel.CurrentPlaylist;

        private bool isPaused;//是否暂停
        private bool isMute;//是否静音
        private WMPLib.WindowsMediaPlayer myplayer;
        private int volume = 0;

        //播放器的更新线程，用于更新播放的时间以及在播放完成之后切换到下一首歌曲
        private BackgroundWorker bgWorker; 

        public MPlayer()
        {
            myplayer = new WindowsMediaPlayer();
        }

        public void StartPlayback()   //从头开始播放
        {
            myplayer.URL = currentTrack.Location;
            myplayer.controls.play();
        }
        
        public void PausePlayback()   //暂停
        {
            myplayer.controls.pause();
            isPaused = true; 
        }
        
        public void ResumePlayback()  //恢复暂停的播放
        {
            myplayer.controls.play();
            isPaused = false;
        }

        public void SetVal(int value)     //设置声音
        {
            myplayer.settings.volume = value; 
        }

        public void Mute()        //静音
        {
            volume = myplayer.settings.volume;
            myplayer.settings.volume = 0;
            isMute = true; 
        }

        public void ReMute()         //恢复静音
        {
            myplayer.settings.volume = volume;
            isMute = false;
        }

        public void StopPlayback()    //停止；
        {
            myplayer.controls.stop();
            
        }
        
        public void SeekTo(double position)          //从给定的位置播放
        {
            myplayer.controls.currentPosition = position;
        }

        public void PlayPreviousTrack()
        {
            currentPlaylist.MoveToPrevious();
            AudioFile af = currentPlaylist.CurrentTrack;

            this.UpdateCurrentTrack(af);
            //停止播放当前曲目;
            StopPlayback();
            StartPlayback();
        }

        public void PlayNextTrack()
        {
            currentPlaylist.MoveToNext();
            AudioFile af = currentPlaylist.CurrentTrack;

            this.UpdateCurrentTrack(af);
            //停止播放当前曲目;
            StopPlayback();
            StartPlayback();
        }

        public bool IsPaused
        {
            get { return isPaused; }
            set { isPaused = value; }
        }
        

        public bool IsMute
        {
            get { return isMute; }
            set { isMute = value; }
        }

        private void UpdateCurrentTrack(AudioFile af)
        {
            currentTrack.Artist = af.Artist;
            currentTrack.Album = af.Album;
            currentTrack.Genre = af.Genre;
            currentTrack.Location = af.Location;
            currentTrack.Year = af.Year;
            currentTrack.Title = af.Title;
            currentTrack.Duration = af.Duration;
        }
    }
}
